<?php
include_once ('../../php/postgre.php');
session_start();
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];
if ($act == 'subkegiatan_action') {
    $id_subkegiatan = $_POST['id_subkegiatan'];
	$id_kegiatan = $_POST['id_kegiatan'];
	$kode_subkegiatan = $_POST['kode_subkegiatan'];
    $nama_subkegiatan = $_POST['nama_subkegiatan'];
    $satuan = $_POST['satuan'];
	$id_rekening = $_POST['id_rekening'];

    if (!is_numeric($id_subkegiatan) || !is_numeric($id_kegiatan) || empty($kode_subkegiatan) || empty($nama_subkegiatan) || empty($satuan) || empty($id_rekening)) {
        echo "Err : invalid input.";
        exit;
    }

    if ($id_subkegiatan > 0) {
        exec_query("update spm_subkegiatan set id_kegiatan = ".$id_kegiatan.",kode_subkegiatan = '".$kode_subkegiatan."',nama_subkegiatan = '".$nama_subkegiatan."',satuan = '".$satuan."',id_rekening = '".$id_rekening."' where id_subkegiatan = ".$id_subkegiatan);
        echo 'success__';
    } else {
        $maxid = get_maxid('id_subkegiatan', 'spm_subkegiatan');
        exec_query("insert into spm_subkegiatan (id_subkegiatan, id_kegiatan, kode_subkegiatan, nama_subkegiatan,satuan,id_rekening) values 
					(" . $maxid . "," . $id_kegiatan . ", '" . $kode_subkegiatan . "', '" . $nama_subkegiatan."','" . $satuan."','" . $id_rekening."' )");
        $st = get_data('select id_subkegiatan from spm_subkegiatan where id_subkegiatan=' . $maxid);
        if ($st['id_subkegiatan'] == $maxid) {
            echo 'success__';
            $id_kegiatan = $maxid;
        }
    }

	echo $id_subkegiatan.'__';
	view_subkegiatan();
}else if($act == 'del_subkegiatan'){
	$id_subkegiatan = $_POST['id_subkegiatan'];
	if(is_numeric($id_subkegiatan)){
		exec_query("DELETE FROM spm_subkegiatan WHERE id_subkegiatan =".$id_subkegiatan);
		echo 'success__';
	}else{
		echo 'Error';
	}
	view_subkegiatan();
} else if ($act == 'edit_subkegiatan') {
    $id_subkegiatan = $_POST['id_subkegiatan'];
    if (is_numeric($id_subkegiatan)) {
        $data = get_data("select id_subkegiatan,id_kegiatan, kode_subkegiatan, nama_subkegiatan,satuan,id_rekening from spm_subkegiatan where id_subkegiatan =" . $id_subkegiatan);
        $ress = implode($data, '__');
        print_r($ress);
        echo "__";
    } else {
        echo 'error';
    }
    view_subkegiatan();
}

function view_subkegiatan() {
				$no=1;
					$subkeg = get_datas("SELECT * FROM spm_rekening r,spm_subkegiatan sb, spm_indikator i,spm_kegiatan k where i.id_skpd=".$_SESSION['_id']." AND i.id_indikator=k.id_indikator AND sb.id_kegiatan=k.id_kegiatan AND sb.id_rekening=r.id_rekening");
					foreach ($subkeg as $subkeg){
					?>
						<tr>
							<td><?php echo $no ?></td>
							<td><?php echo $subkeg['kode_kegiatan'];?></td>
							<td><?php echo $subkeg['nama_kegiatan']?></td>
							<td><?php echo $subkeg['kode_subkegiatan']?></td>
							<td><?php echo $subkeg['nama_subkegiatan']?></td>
							<td><?php echo $subkeg['satuan']?></td>
							<td><?php echo $subkeg['nama_rekening']?></td>
							<td style="width:200px; text-align:center;">
							<a class="btn btn-danger bt-delete-subkegiatan" name="<?php echo $subkeg['id_subkegiatan']; ?>">Delete</a>
                                <a  href="#modalwin" data-toggle="modal"  class="btn btn-info bt-edit-subkegiatan" name="<?php echo $subkeg['id_subkegiatan']; ?>">Edit</a>
							</td>
						</tr>
						<?php 
						$no++;
						}
						}
						?>